#!/usr/bin/env python

import sys
import os.path
from zvm import zmachine, trivialzui

def usage():
    print """Usage: %s <story file>

Run a Z-Machine story under ZVM.
""" % sys.argv[0]
    sys.exit(1)

def main(file_name=''):
    story_file = file_name
    if not file_name:
        if len(sys.argv) != 2:
            usage()
        else:
            story_file = sys.argv[1]
    if not os.path.isfile(story_file):
        print "%s is not a file." % story_file
        usage()
    try:
        with open(story_file, "rb") as f:
            story_image = f.read()
    except IOError:
        print "Error accessing %s" % story_file
        sys.exit(1)

    machine = zmachine.ZMachine(story_image,
                                ui=trivialzui.create_zui(),
                                debugmode=True)
    machine.run()

if __name__ == '__main__':
    main("stories/advent.z5")
